﻿using System;
using System.IO;
using System.Xml;

namespace Goway.IniConifigHelper
{
    /// <summary>
    /// Ini 格式日志接口
    /// </summary>
    public class LoggerInterface
    {
        public static int logFileDeleteFlag;

        public static void WriteLog(string errMsg)
        {
            try
            {
                var Folder = Directory.GetCurrentDirectory() + @"\Interface\" + DateTime.Now.ToString("yyyyMM") + @"\";
                var fileName = Folder + "Interface_" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                if (!Directory.Exists(Folder))
                    Directory.CreateDirectory(Folder);
                if (!File.Exists(fileName))
                {
                    var stream = File.Create(fileName);
                    stream.Close();
                    stream.Dispose();
                }

                using (TextWriter fs = new StreamWriter(fileName, true))
                {
                    fs.Write(errMsg + "\r\n");
                    fs.Close();
                    fs.Dispose();
                }
            }
            catch
            {
            }
        }

        /// <summary>
        /// 删除日志文件，一般删除上上月的
        /// </summary>
        /// <param name="numb"></param>
        public static void DeleteLogFile(int numb)
        {
            if (logFileDeleteFlag == 1)
            {
                //yyyyMMddHHmmssffff
                if (DateTime.Now.ToString("HHmmss") == "122323")
                {
                    logFileDeleteFlag = 0;
                }
            }
            else
            {
                //if (DateTime.Now.ToString("ss") == "24")
                if (DateTime.Now.ToString("mmss") == "2324")
                {
                    try
                    {
                        logFileDeleteFlag = 1;
                        var Folder = Directory.GetCurrentDirectory() + @"\Interface\" +
                                     DateTime.Now.AddMonths(-2).ToString("yyyyMM");
                        if (!Directory.Exists(Folder))
                            return;

                        var dir = new DirectoryInfo(Folder);
                        DeleteDirSub(dir);
                    }
                    catch
                    {
                    }
                }
            }
        }

        public static void DeleteDirSub(DirectoryInfo dir)
        {
            var fileinfo = dir.GetFileSystemInfos();      // 返回目录中所有文件和子目录
            foreach (var item in fileinfo)
            {
                if (item is DirectoryInfo)                   // 判断是否文件夹
                {
                    var subdir = new DirectoryInfo(item.FullName);
                    //subdir.Delete(true);                // 删除子目录和文件
                    DeleteDirSub(subdir);
                }
                else
                {
                    File.Delete(item.FullName);             // 删除指定文件
                }
            }

            dir.Delete(true);
        }
    }

    public static class BaseCfg
    {
        #region 加载配置文件

        public static bool LoadSetting()
        {
            #region 加载配置文件

            try
            {
                var path = AppDomain.CurrentDomain.BaseDirectory + "\\Config\\cfginfo.xml";
                if (!File.Exists(path))
                {
                    /*DBSettingsForm frmDBSettings = new DBSettingsForm();
                    if (frmDBSettings.ShowDialog() == DialogResult.OK)
                        return true;
                    else*/
                    return false;
                }

                var doc = new XmlDocument();
                doc.Load(AppDomain.CurrentDomain.BaseDirectory + "\\Config\\cfginfo.xml");
                var root = (XmlElement)doc.SelectSingleNode("CFG");
                var root1 = (XmlElement)root.SelectNodes("DBCfg")[0];
                LocalDBConnectStr = "Data Source=" + root1.SelectSingleNode("LDBIP").InnerText
                                                   + ";Initial Catalog=" + root1.SelectSingleNode("LDBName").InnerText
                                                   + ";User ID=" + root1.SelectSingleNode("LDBUser").InnerText
                                                   + ";Password=" + root1.SelectSingleNode("LDBPasswd").InnerText
                                                   + ";";

                LocalDBConnectStr2 = "Data Source=" + root1.SelectSingleNode("LDBIP2").InnerText
                                                 + ";Initial Catalog=" + root1.SelectSingleNode("LDBName2").InnerText
                                                 + ";User ID=" + root1.SelectSingleNode("LDBUser2").InnerText
                                                 + ";Password=" + root1.SelectSingleNode("LDBPasswd2").InnerText
                                                 + ";";


                //server=localhost;User Id=root;password=root;Database=tes
                /* ServerDBConnectStr =                    "Data Source=" + root1.SelectSingleNode("SDBIP").InnerText
                                                     + ";Initial Catalog=" + root1.SelectSingleNode("SDBName").InnerText
                                                     + ";User ID=" + root1.SelectSingleNode("SDBUser").InnerText
                                                     + ";Password=" + root1.SelectSingleNode("SDBPasswd").InnerText
                                                     + ";";*/
                MysqlDBConnectStr = $@" server = {root1.SelectSingleNode("SDBIP").InnerText}; User Id = {root1.SelectSingleNode("SDBUser").InnerText}; password = {root1.SelectSingleNode("SDBPasswd").InnerText}; Database = { root1.SelectSingleNode("SDBName").InnerText};Allow User Variables=True;";

                root1 = (XmlElement)root.SelectNodes("Base")[0];
                Factory = root1.SelectSingleNode("Factory").InnerText;
                Workshop = root1.SelectSingleNode("Workshop").InnerText; 

                root1 = (XmlElement)root.SelectNodes("MachineCfg")[0];
                Stacking_IP = root1.SelectSingleNode("Stacking_IP").InnerText;
                Stacking_Port = Convert.ToInt32(root1.SelectSingleNode("Stacking_Port").InnerText);
                Stacking_Device = Convert.ToInt32(root1.SelectSingleNode("Stacking_Device").InnerText);
                Stacking_Rack = Convert.ToInt32(root1.SelectSingleNode("Stacking_Rack").InnerText);
                Stacking_Slot = Convert.ToInt32(root1.SelectSingleNode("Stacking_Slot").InnerText);

                wh_row = root1.SelectSingleNode("WH_ROW").InnerText; 
                root1 = (XmlElement)root.SelectNodes("Printer")[0];
                Printer_IP = root1.SelectSingleNode("Printer_IP").InnerText;
                Printer_Port = Convert.ToInt32(root1.SelectSingleNode("Printer_Port").InnerText);
                Printer_Name = root1.SelectSingleNode("Printer_Name").InnerText;
                Printer_Template = root1.SelectSingleNode("Printer_Template").InnerText;
                return true;
            }
            catch
            {
                return false;
            }

            #endregion
        }

        #endregion

        #region 变量

        public static string Factory { get; set; }
        public static string Workshop { get; set; } 

        public static string LocalDBConnectStr { get; set; }
        public static string LocalDBConnectStr2 { get; set; }
        public static string MysqlDBConnectStr { get; set; }

        public static string Stacking_IP { get; set; }
        public static int Stacking_Port { get; set; }

        public static int Stacking_Device { get; set; }
        public static int Stacking_Rack { get; set; }
        public static int Stacking_Slot { get; set; }

        public static string wh_row { get; set; } 
        

        public static string Printer_IP { get; set; }
        public static int Printer_Port { get; set; }
        public static string Printer_Name { get; set; }
        public static string Printer_Template { get; set; }

        #endregion
    }
}